//
// Created by yangchao on 2022/4/26.
// 21. 合并两个有序链表: https://leetcode-cn.com/problems/merge-two-sorted-lists/
//

#include "../DataStructure/ListNode.h"
using namespace std;

class MergeTwoLists {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode *dummy = new ListNode(-1);
        ListNode *pNode = dummy;
        while (list1 != nullptr && list2 != nullptr) {
            if (list1->val < list2->val) {
                pNode->next = list1;
                list1 = list1->next;
            } else {
                pNode->next = list2;
                list2 = list2->next;
            }
            pNode = pNode->next;
        }
        pNode->next = list1 == nullptr? list2 : list1;
        return dummy->next;
    }
};

